package com.adams.app;

import cn.sharesdk.framework.ShareSDK;

import com.adams.app.common.Global;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.search.MKSearch;

import android.app.Application;
import android.content.Context;
import android.widget.Toast;

/**
 * Created by Issac on 7/18/13.
 */
public class AppData extends Application {

	private BMapManager bMapManager;
	private LocationClient mLocationClient;
	private MKSearch mkSearch;
	public static String flag = Global.FOOD_NAME;
	private static AppData mInstance = null;
	public boolean m_bKeyRight = true;

	@Override
	public void onCreate() {
		super.onCreate();
		ShareSDK.initSDK(this);
		mInstance = this;
		initEngineManager(this);
	}

	private void initEngineManager(AppData appData) {

		if (bMapManager == null) {
			bMapManager = new BMapManager(appData);
		}

		if (!bMapManager.init(Global.MAP_KEY, new MyGeneralListener())) {
			Toast.makeText(AppData.getInstance().getApplicationContext(),
					"BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
		}
		
		if (mLocationClient == null) {
			mLocationClient = new LocationClient(appData);
			mLocationClient.setLocOption(getLocOption(5, 5000));
		}
		
		if (mkSearch == null) {
			mkSearch = new MKSearch();
		}

	}

	public MKSearch getMkSearch() {
		return mkSearch;
	}

	public BMapManager getBmManager() {
		return bMapManager;
	}

	public static Context getContext() {
		return mInstance;
	}

	private LocationClientOption getLocOption(int poiNumber, float poiDistance) {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");
		option.setCoorType("bd09ll");
		option.setScanSpan(5000);
		option.disableCache(true);
		option.setPoiNumber(poiNumber);
		option.setPoiDistance(poiDistance);
		option.setProdName("通过GPS定位我当前的位置");
		option.setPoiExtraInfo(true);
		// option.setPriority(LocationClientOption.GpsFirst);
		return option;
	}

	public void requestLocation() {
		if (!mLocationClient.isStarted()) {
			mLocationClient.start();
		}
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.requestLocation();
		}
	}

	/**
	 * @description 请求poi
	 * 
	 * @author chengbin
	 * 
	 */
	public void requestPoi(int poiNumber, float poiDistance) {
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.setLocOption(getLocOption(poiNumber, poiDistance));
			mLocationClient.requestPoi();
		}
	}

	public BMapManager getMapManager() {
		return bMapManager;
	}

	public MKSearch getSearch() {
		return mkSearch;
	}

	public LocationClient getLocationClient() {
		return mLocationClient;
	}

	public static AppData getInstance() {
		return mInstance;
	}

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	static class MyGeneralListener implements MKGeneralListener {

		@Override
		public void onGetNetworkState(int iError) {
			if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
				Toast.makeText(AppData.getInstance().getApplicationContext(),
						"您的网络出错啦！", Toast.LENGTH_LONG).show();
			} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
				Toast.makeText(AppData.getInstance().getApplicationContext(),
						"输入正确的检索条件！", Toast.LENGTH_LONG).show();
			}
		}

		@Override
		public void onGetPermissionState(int iError) {
			// 非零值表示key验证未通过
			if (iError != 0) {
				// 授权Key错误：
				Toast.makeText(
						AppData.getInstance().getApplicationContext(),
						"map key error: "
								+ iError, Toast.LENGTH_LONG).show();
				AppData.getInstance().m_bKeyRight = false;
			} else {
				AppData.getInstance().m_bKeyRight = true;
			}
		}
	}

}
